/**
 * Created by tongjunshui on 17/6/9.
 */
var express = require('express');
var router = express.Router();

var settings = require('../../settings');
var async = require('async');

var moment = require('moment');
var superagent = require('superagent');
var dbDao = require('../../models/dbBase.js');


router.get('/gym', function (req,res){
    var sql = "select * from Gymnasium";
    dbDao.find( sql, function (err, result) {
        if(err){
            res.send({
                    result:'error',
                    message: '查询失败'
            })
        }else{
            res.send({
                result:'success',
                    data: result
            })
        }
    });
});

router.get('/fields/findFree', function (req,res){
    var fieldId = req.query.fieldId || '';
    var startTime = req.query.startTime || '';
    var hours = req.query.hours || '';
    var date = req.query.date || '';

    superagent.get(settings.gymServiceApiBaseUrl+'/prebook/findFreeResource')
        .query('fieldId='+fieldId)
        .query('startTime='+startTime)
        .query('hours='+hours)
        .query('date='+date)
        .end(function(err, result){
            if(err){

                console.log("error:", err);
                res.send({result:'error'});
            }else{
                if(result&&result.body){
                    console.log(result.body);
                    if(result.body.data){
                        res.send({result:'success',data:result.body.data});
                    }else{
                        res.send({result:'error',message:'无可用场地'})
                    }
                }else{
                    res.send({result:'error',message:'无可用场地'})
                }
            }
        });
});


//场馆详情
router.get('/gym/:id', function (req,res){
    console.log("进入资源详情页");
    var gymId = req.params.id;
    var sql = "select * from GymFields where gymId='" + gymId + "'";
    var gyminfo;   //场馆信息
    var fieldinfo = [];   //场馆的场地信息  和  日期价格

    async.auto({
        findGymium: function (callback) {  //场馆信息
            dbDao.findById( ["Gymnasium", "id", gymId], function (err, result) {
                gyminfo = result;
                callback();
            });
        },
        findGymfield: function (callback) {    //场馆场地信息
            dbDao.find( sql, function (err, result2) {
                if (result2.length > 0) {
                    var count = 0;
                    for (var i = 0; i < result2.length; i++) {
                        //var sql2 = "select * from GymResource where gymFieldId='" + gymFieldId + "' and gymId='" + gymId + "'";
                        fieldinfo.push(result2[i]);
                    }
                    callback();
                } else {
                    callback();
                }
            });
        },

    }, function (err, results) {
        res.send({result:'success',data:{
                gymInfo: gyminfo,
                fields: fieldinfo,
            }}
        )

    });
})


////获取场地资源
//router.get('/gym/:id/:fieldId', function (req,res){
//    var gymId = req.params.id;
//    var gymFiedid = req.params.fiedid;
//    var ddate = req.query.date || moment().format('YYYYMMDD');
//
//    var sql = "select * from GymResource where date='" + ddate + "' and  gymId='" + gymId + "' and gymFieldId='" + gymFiedid + "' ";
//
//    dbDao.find( sql, function (err, result2) {
//        if(err){
//            res.send({result:'error',message:'查询失败'});
//        }else{
//            console.log(result2.length);
//            res.send(result2);
//        }
//    });
//})

module.exports = router;